package com.zyh.thread.demo1;

/**
 * @Author zyh
 * @Date 2024/8/8 9:21
 * @Description 取钱任务1
 */

public class Task01 implements Runnable{
    private Account account;

    public Task01() {
    }

    public Task01(Account account) {
        this.account = account;
    }

    @Override
    public void run() {
        synchronized (account) {
            //模拟取钱
            if (account.getMoney()>3000){
                //模拟取钱时间
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //开始取钱
                account.setMoney(account.getMoney()-3000);
                System.out.println(Thread.currentThread().getName()+"取了3000，余额还剩下"+account.getMoney());
            }
            else {
                System.out.println(Thread.currentThread().getName()+"取钱时余额不足，还剩"+account.getMoney());
            }
        }
    }
}
